<?php

namespace yiike\credit\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\behaviors\BlameableBehavior;
use \yiike\credit\models\base\CreditRecord as BaseCreditRecord;
use \yiike\user\models\User;

/**
 * This is the model class for table "credit_record".
 */
class CreditRecord extends BaseCreditRecord {

    /**
     * @inheritdoc
     * @return type array
     */
    public function behaviors() {
        return [
            [
                'class'              => TimestampBehavior::className(),
                'createdAtAttribute' => 'created_at',
                'updatedAtAttribute' => null,
            ],
            [
                'class'              => BlameableBehavior::className(),
                'createdByAttribute' => 'created_by',
                'updatedByAttribute' => null,
            ],
        ];
    }


    public function beforeSave($insert) {
        return parent::beforeSave($insert);
    }

    public function getUserName() {
        $user = User::findOne($this->user_id);
        return $user->username;
    }

    public function getValue1Text() {
        if ($this->value1 * 100 == 0)
            return '';
        return ($this->user_id == Yii::$app->user->id ? '+' : '-') . $this->value1;
    }

    public function getValue2Text() {
        if ($this->value2 * 100 == 0)
            return '';
        return ($this->user_id == Yii::$app->user->id ? '+' : '-') . $this->value2;
    }

    public function getValue3Text() {
        if ($this->value3 * 100 == 0)
            return '';
        return ($this->user_id == Yii::$app->user->id ? '+' : '-') . $this->value3;
    }

}
